package com.molyfun.parents.im;

import android.util.Log;

import com.molyfun.parents.im.model.BaseMessageEntity;
import com.molyfun.parents.utils.GsonUtil;
import com.tencent.imsdk.TIMElem;
import com.tencent.imsdk.TIMElemType;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMMessageListener;
import com.tencent.imsdk.TIMTextElem;

import java.util.LinkedList;
import java.util.List;

/**
 * 腾讯im消息监听
 * Created by ligang on 2018/1/15 0015.
 *
 * room start --> live start
 room stop  --> live stop
 room errorstop - > live errorstop
 meeting invite --> live meetinginvite
 meeting invitecancel -- > live meetinginvitecancel
 meeting start  --> live meetingstart
 meeting stop   --> live meetingstop
 meeting apply  -->live meetingapply
 meeting cancel	-->live meetingapplycancel
 meeting confirm -->live meetingconfim
 msg send       --> live groupmsg
 gallery open    -->live  galleryopen
 gallery close    -->live galleryclose
 gallery seek     -->live galleryseek
 gallery pause     -->live gallerypause


 room enter --> live enter
 room leave --> live leave

 新增
 小组群聊
 group groupmsg
 group singlemsg
 group livestart
 group livestop

 咨询
 customplan groupmsg
 customplan refreshcount

 登陆
 login  imloginout

 直播事件

 现有的事件:
 eventtype:live    opertype:galleryopen       //素材打开
 opertype:galleryclose      //素材关闭
 opertype:gallerypause      //素材暂停
 opertype:galleryseek       //素材快进
 *
 */

public class MyTIMMsgListener implements TIMMessageListener {

    @Override
    public boolean onNewMessages(List<TIMMessage> list) {

        if (list != null && list.size() > 0) {
            for (TIMMessage timMessage : list) {
                if (timMessage.getElementCount() > 0) {
                    TIMTextElem msg;
                    for (int i = 0; i < timMessage.getElementCount(); i++) {
                        TIMElem elem = timMessage.getElement(i);
                        if (elem.getType() == TIMElemType.Text) {
                            try {
                                msg = (TIMTextElem) elem;
                                String txt = msg.getText();
                                Log.d("loginTIM-msg-->", txt);

                                if (mITIMMsgReceivedCallbacks != null) {
                                    BaseMessageEntity[] data = GsonUtil.fromJson(msg.getText(), BaseMessageEntity[].class);//JSON.parseArray(msg.getText(), BaseMessageEntity.class);
                                    for (BaseMessageEntity messageEntity : data) {
                                        for(ITIMMsgReceivedCallback itimMsgReceivedCallback : mITIMMsgReceivedCallbacks){
                                            itimMsgReceivedCallback.onMessage(messageEntity);
                                        }
                                    }
                                }
                            } catch (Exception e) {
                                Log.e("loginTIM-msg-->", e.toString());
                            }
                        } else {
//                            readMsg(timMessage);
                        }
                    }
                }
            }
        }
        return true;

    }

    public interface EventType{
        String CUSTOMPLAN = "customplan";
        String LOGIN = "login";
        //小组群聊
        String GROUP = "group";
        //直播
        String LIVE = "live";
        //新版im消息
        String IM_MSG = "msg";
    }

    public interface OperType{
        String GROUPMSG = "groupmsg";
        String REFRESH_COUNT = "refreshcount";
        String IM_LOGINOUT = "imloginout";
        String GROUP_MSG = "groupmsg";
        String SINGLE_MSG = "singlemsg";
        String LIVE_START = "livestart";
        String LIVE_STOP = "livestop";
    }

    public static void addITIMMsgReceivedCallback(ITIMMsgReceivedCallback itimMsgReceivedCallback){
        if(!mITIMMsgReceivedCallbacks.contains(itimMsgReceivedCallback)){
            mITIMMsgReceivedCallbacks.add(itimMsgReceivedCallback);
        }
    }

    public static void removeITIMMsgReceivedCallback(ITIMMsgReceivedCallback itimMsgReceivedCallback){
        if(mITIMMsgReceivedCallbacks.contains(itimMsgReceivedCallback))
        mITIMMsgReceivedCallbacks.remove(itimMsgReceivedCallback);
    }

    private static List<ITIMMsgReceivedCallback> mITIMMsgReceivedCallbacks = new LinkedList<>();

    public interface ITIMMsgReceivedCallback {
        void onMessage(BaseMessageEntity messageEntity);
    }

    public static class MsgEventBean {
        private String eventtype;
        private String opertype;
        private String conversationId;

        public String getConversationId() {
            return conversationId;
        }

        public void setConversationId(String conversationId) {
            this.conversationId = conversationId;
        }

        public String getEventtype() {
            return eventtype;
        }

        public void setEventtype(String eventtype) {
            this.eventtype = eventtype;
        }

        public String getOpertype() {
            return opertype;
        }

        public void setOpertype(String opertype) {
            this.opertype = opertype;
        }
    }
}
